#!/bin/bash
# Copyright 2012 Twitter, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# Wrapper around Pig that does the following:
#
# - Inserts our custom pig build into the classpath
# - Adds Ambrose to the classpath
# - Starts Pig with embedded Ambrose PPNL
#

function log() { echo "$@" >&2; }
function die() { log "$@"; exit 1; }

# configure args
AMBROSE_PIG_BIN="${AMBROSE_PIG_BIN:-pig}"
AMBROSE_HOME="${AMBROSE_HOME:-$(cd $(dirname "$0")/..; pwd -P)}"
AMBROSE_PORT="${AMBROSE_PORT:-random}"
AMBROSE_TIMEOUT="${AMBROSE_TIMEOUT:-600}" # 10 min

# test for presence of pig command
which "$AMBROSE_PIG_BIN" >/dev/null 2>&1 || die $(cat <<EOF

Command '$AMBROSE_PIG_BIN' not found. This script requires a local installation of Apache
Pig. Please ensure \$PIG_HOME/bin is in your \$PATH, or export AMBROSE_PIG_BIN to reference a
particular pig script:

export AMBROSE_PIG_BIN=/path/to/my/pig/bin/pig

EOF
)

# configure paths
AMBROSE_JARS=$(find "$AMBROSE_HOME/lib" -name '*.jar' -exec printf '%s:' '{}' '+') \
    || die "Failed to find ambrose jars within path '$AMBROSE_HOME/lib'"
ADDITIONAL_JARS=$(echo "$AMBROSE_JARS" | perl -n -e 'print join ":", grep {/guava/} split(/:/)') \
    || die "Failed to find additional jars within path '$AMBROSE_HOME/lib'"
export PIG_CLASSPATH="$AMBROSE_JARS$PIG_CLASSPATH"
export HADOOP_USER_CLASSPATH_FIRST="true"


# configure the ambrose pig notification listener and port
export PIG_OPTS="\
-Dpig.notification.listener=com.twitter.ambrose.pig.EmbeddedAmbrosePigProgressNotificationListener \
-Dpig.additional.jars=$ADDITIONAL_JARS \
-Dambrose.port=$AMBROSE_PORT \
-Dambrose.post.script.sleep.seconds=$AMBROSE_TIMEOUT \
$PIG_OPTS"

# construct pig command line, log and invoke
log "PIG_CLASSPATH=$PIG_CLASSPATH"
log "PIG_OPTS=$PIG_OPTS"
exec pig $@
